package com.huiquan.section.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.section.service.SectionImageService;

@Controller
public class SectionImageController {

    @Autowired
    private SectionImageService sectionImageService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/section/section_image_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		Integer start = RequestParamUtil.getIntegerDefaultDi(req, "start", 1);
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameKey");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");
		String className = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "className");

		return sectionImageService.list(start+"", codeSearch, nameSearch,countPerPageStr,className);
	}
	/**
	 * 添加科室图片
	 *
	 * @param req
	 * @param resp
	 * @param multipartRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/section/insert_section_image.do")
	public ReturnData InsertSectionImage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile image) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String name = req.getParameter("name");
		String rank = req.getParameter("rank");
		String nameClass = req.getParameter("nameClass");
		ReturnData rtn = null;
		try {
			rtn = sectionImageService.insert(name,rank,nameClass,user, image);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
		
	/**
	 * 修改疾病图片
	 *
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/section/update_image_data.do")
	public ReturnData updateSectionImage(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		String name = req.getParameter("name");
		String departmentClass = req.getParameter("departmentClass");
		String imageName = req.getParameter("imageName");
		String rank = req.getParameter("rank");

		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = null;

		try {
			rtn = sectionImageService.update(id, name, departmentClass, imageName, rank, user);
		} catch (Exception e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		
		
		return rtn;
	}
	
	/**
	 * 更新科室图片
	 *
	 * @param req
	 * @param resp
	 * @param multipartRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/section/update_section_image.do")
	public ReturnData update(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile file) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");

		return sectionImageService.updateImage(id,user, file);
	}
	
	/**
	 * 删除一条记录
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/section/delete_section_image.do")
	public ReturnData deleteTestingClass(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String id = req.getParameter("id");
		ReturnData rtn = null;
		try {
			rtn = sectionImageService.deleteClass(id);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}

	/**
	 * 批量导入科室图片
	 *
	 * @param req
	 * @param resp
	 * @param multipartRequest
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value = "/section/batch_add_section_image.do")
	public ReturnData batchInsertSectionImage(HttpServletRequest req, HttpServletResponse resp,
			@RequestParam(required = false) MultipartFile batchFile) throws IOException {
		User user = (User) req.getSession().getAttribute("user");
		ReturnData rtn = null;

		try {
			rtn = sectionImageService.insertBatch(user, batchFile);
		} catch (RuntimeException e) {
			ReturnCode rc = ReturnCode.DYNAMIC_DESC_EXCEPTION;
			rc.setDesc(e.getMessage() );
			return ReturnUtil.fail(rc);
		}
		return rtn;
	}
	
	/**
	 * 初始化rank
	 * 
	 * @param req
	 * @param resp
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping("/section/init_section_image_rank.do")
	public ReturnData initSectionImageRank(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String nameClass = req.getParameter("nameClass");
		return sectionImageService.initSectionImageRank(nameClass);
	}
	
	/**
	 * 移动检验大类的位置，调整其排序
	 * 
	 * @param req
	 * @param resp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/section/move_section_image_rank.do")
	public ReturnData moveTestingClass(HttpServletRequest req, HttpServletResponse resp) {
		User user = (User) req.getSession().getAttribute("user");
		String id = req.getParameter("id");
		String flag = req.getParameter("flag");
		return sectionImageService.move(user, id, Integer.valueOf(flag));
	}
}
